package com.example.hotdog;

import cn.hutool.core.map.CustomKeyMap;
import com.example.hotdog.entity.*;
import com.example.hotdog.httpu.HttpReHot;
import com.example.hotdog.task.HotTask;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class HelloController{

    public static ConcurrentMap userMap = new ConcurrentHashMap();
    public static ConcurrentMap<String,Boolean> amountMap = new ConcurrentHashMap<>();
    public static int maxRenwu = 20;
    public static int maxXiancheng = 5;

    @FXML
    private Button button1;

    @FXML
    private Button button2;

    @FXML
    private Button button3;

    @FXML
    private TextField input1;

    @FXML
    private TextField input2;

    @FXML
    private TextField input3;

    @FXML
    private TextField input4;

    @FXML
    private TextField input5;

    @FXML
    private TextField input6;

    @FXML
    private TextField input7;

    @FXML
    private TextField input8;

//    @FXML
//    private TextField input9;

    @FXML
    private TableColumn<?, ?> t1col1;

    @FXML
    private TableColumn<?, ?> t1col2;

    @FXML
    private TableColumn<?, ?> t1col3;

    @FXML
    private TableColumn<?, ?> t1col4;

    @FXML
    private TableColumn<?, ?> t1col5;

    @FXML
    private TableColumn<?, ?> t1col6;

    @FXML
    private TableColumn<?, ?> t1col7;

    @FXML
    private TableColumn<?, ?> t1col8;

    @FXML
    private TableColumn<?, ?> t2col1;

    @FXML
    private TableColumn<?, ?> t2col2;

    @FXML
    private TableColumn<?, ?> t2col3;

    @FXML
    private TableColumn<?, ?> t2col4;

    @FXML
    private TableColumn<?, ?> t2col5;

    @FXML
    private TableColumn<?, ?> t2col6;

    @FXML
    private TableColumn<?, ?> t3col1;

    @FXML
    private TableColumn<?, ?> t3col2;

    @FXML
    private TableColumn<?, ?> t3col3;

    @FXML
    private TableColumn<?, ?> t3col4;

    @FXML
    private TableColumn<?, ?> t3col5;

    @FXML
    private TableColumn<?, ?> t4col1;

    @FXML
    private TableColumn<?, ?> t4col2;

    @FXML
    private TableColumn<?, ?> t4col3;

    @FXML
    private TableColumn<?, ?> t4col4;

    @FXML
    private TableView<Renwu> table1;

    @FXML
    private TableView<History> table2;

    @FXML
    private TableView<Success> table3;

    @FXML
    private TableView<Amount> table4;


    @FXML
    void button1click(ActionEvent event) throws IOException {
        String rrr = "17721879070----d080cac004b94e41ad1ec084f02ca5ff----773210----3130&1021373----10888----1----0----正在执行----wxid_k6sjoq5w8poh12&wxid_kdo8223snxir12----快付通";
        File file = new File("./dataf/renwu.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file,true));
        bufferedWriter.write(rrr);
        bufferedWriter.newLine();
        bufferedWriter.flush();
//        table1.getItems().add(renwu);
    }

    @FXML
    void button2click(ActionEvent event) throws IOException, IllegalAccessException {
//        System.out.println(input1.getText());
//        System.out.println(input2.getText());
//        System.out.println(input3.getText());
//        System.out.println(input4.getText());
//        System.out.println(input5.getText());
//        System.out.println(input6.getText());
//        System.out.println(input7.getText());
//        System.out.println(input8.getText());

        if (Objects.equals("",input1.getText()) || Objects.equals(null,input1.getText())
            || Objects.equals("",input2.getText()) || Objects.equals(null,input2.getText())
                || Objects.equals("",input3.getText()) || Objects.equals(null,input3.getText())
                || Objects.equals("",input4.getText()) || Objects.equals(null,input4.getText())
                || Objects.equals("",input5.getText()) || Objects.equals(null,input5.getText())
                || Objects.equals("",input6.getText()) || Objects.equals(null,input6.getText())
                || Objects.equals("",input7.getText()) || Objects.equals(null,input7.getText())



        ){
            System.out.println("hello");


        }else {
            //执行逻辑
            userMap.put("username",input1.getText());
            userMap.put("passw",input2.getText());
            userMap.put("zhiPassw",input3.getText());
            userMap.put("proId",input4.getText());
            userMap.put("suoNum",input5.getText());
            userMap.put("suoPrice",input6.getText());

            startApp();
        }

    }

    private void startApp() throws IOException, IllegalAccessException {

        maxRenwu = Integer.parseInt(input7.getText());
        maxXiancheng = Integer.parseInt(input8.getText());

        renwuRead();
        amountRead();

        File file4 = new File("./dataf/amount.txt");
        BufferedReader bufferedReader4 = new BufferedReader(new FileReader(file4));
        String temp4;
        int i4 = 1;
        while((temp4 = bufferedReader4.readLine()) != null){
            String[] split4 = temp4.split("----");
            Amount amount = new Amount("" + i4,split4[0],split4[1],split4[2]);
            i4++;
            ObservableList<Renwu> items = table1.getItems();
            if (!items.isEmpty()){
                Renwu renwu = items.get(0);
                String t1col21 = renwu.getT1col3();
                String[] split = t1col21.split("&");
                List<Map> mapList = HttpReHot.postQ(amount.getT4col3(),split[1],split[0],renwu.getT1col4());
                if (!mapList.isEmpty()){
                    amountMap.put(amount.getT4col3(),true);
                }else {
                    amountMap.remove(amount.getT4col3());
                }
            }




        }
        amountUpdate();

        ObservableList<Renwu> items = table1.getItems();

        items.forEach(i->{
            HotTask.addTask(i);
        });

    }

    private void amountUpdate() throws IOException, IllegalAccessException {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        File file4 = new File("./dataf/amount.txt");
                        BufferedReader bufferedReader4 = new BufferedReader(new FileReader(file4));
                        String temp4;
                        int i4 = 1;
                        while((temp4 = bufferedReader4.readLine()) != null){
                            String[] split4 = temp4.split("----");
                            Amount amount = new Amount("" + i4,split4[0],split4[1],split4[2]);
                            i4++;
                            ObservableList<Renwu> items = table1.getItems();
                            if (!items.isEmpty()){
                                Renwu renwu = items.get(0);
                                String t1col21 = renwu.getT1col3();
                                String[] split = t1col21.split("&");
                                List<Map> mapList = HttpReHot.postQ(amount.getT4col3(),split[1],split[0],renwu.getT1col4());
                                if (!mapList.isEmpty()){
                                    amountMap.put(amount.getT4col3(),true);
                                }else {
                                    amountMap.remove(amount.getT4col3());
                                }
                            }


                        }
                        Thread.sleep(5000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        //开启线程
        thread.start();



    }

    private void amountRead(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        ObservableList<Amount> list4 = FXCollections.observableArrayList();
                        File file4 = new File("./dataf/amount.txt");
                        BufferedReader bufferedReader4 = new BufferedReader(new FileReader(file4));
                        String temp4;

                        int i4 = 1;
                        while((temp4 = bufferedReader4.readLine()) != null){
                            String[] split4 = temp4.split("----");
                            Amount amount = new Amount("" + i4,split4[0],split4[1],split4[2]);
                            i4++;
                            list4.add(amount);

                        }
                        bufferedReader4.close();
                        table4.setItems(list4);
                        Thread.sleep(5000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        //开启线程
        thread.start();
    }

    private void renwuRead(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        ObservableList<Renwu> list5 = FXCollections.observableArrayList();
                        File file = new File("./dataf/renwu.txt");
                        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                        String temp;
                        int j = 1;
                        while((temp = bufferedReader.readLine()) != null){
                            String[] split = temp.split("----");
                            if (split.length < 5){
                                continue;
                            }
                            Renwu renwu = new Renwu( "" + j, split[0],split[3], split[4], split[5], split[6], split[7],split[9],split[1]);
                            j++;
                            list5.add(renwu);

                        }
                        bufferedReader.close();
                        table1.getItems().removeAll();
                        table1.setItems(list5);

                        list5.forEach(i->{
                            HotTask.addTask(i);
                        });

                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (FileNotFoundException e) {
                        throw new RuntimeException(e);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        //开启线程
        thread.start();
    }

    @FXML
    void button3click(ActionEvent event) {


        System.out.println(amountMap);

    }



    @FXML
    void initialize() throws IOException {


        input7.setText("20");
        input8.setText("5");


        ObservableList<Renwu> list1 = FXCollections.observableArrayList();
        ObservableList<History> list2 = FXCollections.observableArrayList();
        ObservableList<Success> list3 = FXCollections.observableArrayList();
        ObservableList<Amount> list4 = FXCollections.observableArrayList();


        File file = new File("./dataf/renwu.txt");
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String temp;
        int i = 1;
        while((temp = bufferedReader.readLine()) != null){
            String[] split = temp.split("----");
            if (split.length < 5){
                continue;
            }
            Renwu renwu = new Renwu( "" + i, split[0],split[3], split[4], split[5], split[6], "未执行",split[9],split[1]);
            i++;
            list1.add(renwu);

        }
        bufferedReader.close();


        File file4 = new File("./dataf/amount.txt");
        BufferedReader bufferedReader4 = new BufferedReader(new FileReader(file4));
        String temp4;
        int i4 = 1;
        while((temp4 = bufferedReader4.readLine()) != null){
            String[] split4 = temp4.split("----");
            Amount amount = new Amount("" + i4,split4[0],split4[1],split4[2]);
            i4++;
            list4.add(amount);


        }
        bufferedReader4.close();



        History history = new History("1","2","3","4","5","6");
        Success success = new Success("1","2","3","4","5");



        list2.add(history);
        list3.add(success);


        t1col1.setCellValueFactory(new PropertyValueFactory<>("t1col1"));
        t1col2.setCellValueFactory(new PropertyValueFactory<>("t1col2"));
        t1col3.setCellValueFactory(new PropertyValueFactory<>("t1col3"));
        t1col4.setCellValueFactory(new PropertyValueFactory<>("t1col4"));
        t1col5.setCellValueFactory(new PropertyValueFactory<>("t1col5"));
        t1col6.setCellValueFactory(new PropertyValueFactory<>("t1col6"));
        t1col7.setCellValueFactory(new PropertyValueFactory<>("t1col7"));
        t1col8.setCellValueFactory(new PropertyValueFactory<>("t1col8"));
        t2col1.setCellValueFactory(new PropertyValueFactory<>("t2col1"));
        t2col2.setCellValueFactory(new PropertyValueFactory<>("t2col2"));
        t2col3.setCellValueFactory(new PropertyValueFactory<>("t2col3"));
        t2col4.setCellValueFactory(new PropertyValueFactory<>("t2col4"));
        t2col5.setCellValueFactory(new PropertyValueFactory<>("t2col5"));
        t2col6.setCellValueFactory(new PropertyValueFactory<>("t2col6"));
        t3col1.setCellValueFactory(new PropertyValueFactory<>("t3col1"));
        t3col2.setCellValueFactory(new PropertyValueFactory<>("t3col2"));
        t3col3.setCellValueFactory(new PropertyValueFactory<>("t3col3"));
        t3col4.setCellValueFactory(new PropertyValueFactory<>("t3col4"));
        t3col5.setCellValueFactory(new PropertyValueFactory<>("t3col5"));
        t4col1.setCellValueFactory(new PropertyValueFactory<>("t4col1"));
        t4col2.setCellValueFactory(new PropertyValueFactory<>("t4col2"));
        t4col3.setCellValueFactory(new PropertyValueFactory<>("t4col3"));
        t4col4.setCellValueFactory(new PropertyValueFactory<>("t4col4"));


        table1.setItems(list1);
        table2.setItems(list2);
        table3.setItems(list3);
        table4.setItems(list4);


    }

//    public void initialize(URL url, ResourceBundle resourceBundle) {
//        ObservableList<User> list = FXCollections.observableArrayList();
//        System.out.println("hello");
//        User user = new User();
//        user.setCol1("11");
//        user.setCol2("22");
//        col1.setCellValueFactory(new PropertyValueFactory<User,String>("col1"));
//        col2.setCellValueFactory(new PropertyValueFactory<User,String>("col2"));
//        list.add(user);
//        table1.setItems(list);
//    }
}